package so.ifl.manager.admin;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import so.ifl.servcie.RoleService;
import so.ifl.vo.RoleVo;

/**
 *create by aaron.lu
 *create at 2012-1-15 上午1:04:32
 *<mailto: atyuan.lu@gmail.com>
 **/
@Component("roleManager")
public class RoleManager {
	
	@Autowired
	private RoleService roleService;
	
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 查询角色
	 * create by aaron.lu
	 * create at 2012-1-15 上午1:17:07
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param map
	 *
	 */
	@DataProvider
	public void getRoles(Page<RoleVo> page, Map<String, Object> parameter){
		int offset = page.getFirstEntityIndex();
		int limit = page.getPageSize();
		
		List<RoleVo> list = roleService.queryRoleWithParamAndPagination(parameter, offset, limit);
		int tolal = roleService.countRoleWithParam(parameter);
		
		logger_.debug(list.size() + " roles got.. ");
		page.setEntities(list);
		page.setEntityCount(tolal);
	}
	
	/**
	 * 角色保存方法
	 * create by aaron.lu
	 * create at 2012-1-15 上午11:58:12
	 * <mailto: atyuan.lu@gmail.com>
	 * @param roles
	 *
	 */
	@DataResolver
	public void saveRole(Collection<RoleVo> roles){
		for (RoleVo roleVo : roles) {
			EntityState entityState = EntityUtils.getState(roleVo);
			if (EntityState.NEW.equals(entityState)) {
				//新增
				roleService.insertRole(roleVo);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				//修改-角色无需进行修改
			} else if (EntityState.DELETED.equals(entityState)) {
				//删除
				roleService.deleteRole(roleVo.getRoleId());
			}
		}
	}
}
